package com.shark;

import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
/**
 * @auther shark
 * @create 2024-08-09 22:39
 *
 * 如果队列和消息都设置了超时时间控制，谁短就听谁的
 */
@SpringBootTest
public class RabbitMQTest4TTL
{
    @Resource
    RabbitTemplate rabbitTemplate;

    @Test
    public void testSendMessageTTL()
    {
        // 1、创建消息后置处理器对象,单独给每一个message设置超时时间,消息层面设置
        MessagePostProcessor messagePostProcessor = (Message message) -> {
            // 设定 TTL 时间，以毫秒为单位
            message.getMessageProperties().setExpiration("5000");
            return message;
        };



        // 2、发送消息
        rabbitTemplate.convertAndSend(
                "exchange.direct.order",
                "stock",
                "Hello atguigu ttl",messagePostProcessor);
    }
}
